'use strict';

const Controller = require('egg').Controller;

class UserController extends Controller {
  async index(ctx) {
    let userId = ctx.session.userId;
    let user = await ctx.model.Sysuser.findById(userId, {});
    if (!user) {
      ctx.status = 501;
      ctx.body = {
        msg: '无此用户数据'
      }
      return;
    }
    user.password = null;
    ctx.body = user;
  }
  async edit(ctx) {
    let userId = ctx.session.userId;
    let user = await ctx.model.Sysuser.findById(userId, {});
    if (!user) {
      ctx.status = 501;
      ctx.body = {
        msg: '无此用户数据'
      }
      return;
    }
    let param = ctx.request.body;
    let result = await ctx.model.Sysuser.update({ _id: userId }, { $set: param });
    ctx.body = {
      msg: result.ok > 0 ? '修改成功' : '修改失败'
    }
  }
}

module.exports = UserController;
